package com.example.sharedpreferencedemo;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.content.SharedPreferencesCompat;

public final class SPCache {
    private static String TAG = SPCache.class.getSimpleName();

    // 隐藏两个参数
    private Context context;
    private SharedPreferences sp;

    // 保存传入的文件名参数
    private String fileName;

    // 禁止无参默认构造函数
    private SPCache() {}

    // 统一确定两个参数，一个fileName，对应于内部一个sp
    public SPCache(@NonNull Context context, @NonNull String fileName) {
        this.context = context.getApplicationContext();
        this.sp = this.context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        this.fileName = fileName;
    }

    /**
     * 存入缓存，类型可以是int，long，float，Boolean，String；自定义对象会通过toString()转换为字符串
     * @param key     键
     * @param object  数据
     */
    public void put(@NonNull String key, Object object) {
        SharedPreferences.Editor editor = sp.edit();

        // instanceof 用来 指出对象是否是特定类的一个实例
        if (object instanceof String) {
            editor.putString(key, (String) object);
        }
        else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        }
        else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        }
        else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        }  else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            editor.putString(key, object.toString());
        }

        editor.commit();

        Log.v(TAG, "写入;键:" + key + " 数据：" + object);
    }

    /**
     * 根据默认值类型，获取相应的数据。类型可以是int，long，float，Boolean，String；
     * @param key               键
     * @param defaultObject     默认值
     * @return                  数据
     */
    public Object get(@NonNull String key, @NonNull Object defaultObject) {
        if (defaultObject instanceof String) {
            return sp.getString(key, (String) defaultObject);
        }
        else if (defaultObject instanceof Integer) {
            return sp.getInt(key, (Integer) defaultObject);
        }
        else if (defaultObject instanceof Boolean) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        }
        else if (defaultObject instanceof Float) {
            return sp.getFloat(key, (Float) defaultObject);
        }
        else if (defaultObject instanceof Long) {
            return sp.getLong(key, (Long) defaultObject);
        }
        return null;
    }

    /**
     * 移除键对应的数据
     * @param key  键
     */
    public void remove(@NonNull String key) {
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
        Log.v(TAG, "移除;键:" + key);
    }

    /**
     * 清空缓存
     */
    public void clear() {
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
        Log.v(TAG, "清空SharedPreferences，文件名：" + fileName);
    }
}
